package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.SelectPresentationBean;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static android.view.Gravity.BOTTOM;

/**
 * 提现界面
 */
public class WithdrawalActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_balance_all)
    TextView tvBalanceAll;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.btn_click)
    TextView btnClick;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.tv_alipay_name)
    TextView tvAlipayName;

    BusinessDao mod5Dao;
    String alipayName, alipayAccount;

    @Override
    public void init() {
        tvTitle.setText("提现");
        tvBalance.setText("可用余额" + MainBActivity.User.getBalance() + "元");
        mod5Dao = new BusinessDao(this, this);
        mod5Dao.SelectPresentation(MainBActivity.User.getUserId());
    }

    @Override
    public int setLayout() {
        return R.layout.activity_withdrawal;
    }

    boolean isFirst;

    @Override
    public void initLoadStart() {
        if (isFirst) {
            loading.show();
        }
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 查看支付宝账号:
                SelectPresentationBean bean = (SelectPresentationBean) bindingViewBean.getBean();
                if (bean.getCode().equals("0000000")) {
                    SelectPresentationBean.DataBean data = bean.getData();
                    if (data.getAlipayAccount() != null && !data.getAlipayAccount().equals("")) {
                        llAlipay.setVisibility(View.VISIBLE);
                        alipayName = data.getAlipayName();
                        alipayAccount = data.getAlipayAccount();
                        tvAlipayName.setText(data.getAlipayName() + "(" + data.getAlipayAccount() + ")");

                    } else {
                        tvAdd.setText("添加");
                        llAlipay.setVisibility(View.GONE);
                    }

                }
                break;

            case 更新支付宝账号:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if (pubStatusBean.getCode().equals("0000000")) {
                    popupWindow.dismiss();
                    mod5Dao.SelectPresentation(MainBActivity.User.getUserId());
                } else {
                    Toasty.normal(activity, pubStatusBean.getMsg()).show();
                }
                break;

            case 提现:
                PubStatusBean statusBean = (PubStatusBean) bindingViewBean.getBean();
                if (statusBean.getCode().equals("0000000")) {

                    startActivity(new Intent(activity, WithdrawalEndActivity.class)
                            .putExtra("alipayName", alipayName)
                            .putExtra("alipayAccount", alipayAccount)
                            .putExtra("msg", statusBean.getMsg())
                            .putExtra("type", statusBean.getData().isResult() ? 1 : 0)
                            .putExtra("num", etNum.getText().toString()));
                    finish();

                }else {
                    Toasty.normal(activity,statusBean.getMsg()).show();
                }
                break;
        }

    }

    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;

    private void showPop() {
        Tools.lightoff(activity);

        popupView = View.inflate(activity, R.layout.pop_add_user, null);
        popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tools.lighton(activity);
            }
        });

        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);

        ImageView imgClose = popupView.findViewById(R.id.img_close);
        final EditText etAccount = popupView.findViewById(R.id.et_account);
        final EditText etName = popupView.findViewById(R.id.et_name);
        TextView textView = popupView.findViewById(R.id.tv_submission);

        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!etAccount.getText().toString().trim().equals("")
                        || !etName.getText().toString().trim().equals("")) {
                    mod5Dao.AddAlipay(MainBActivity.User.getUserId(),
                            etAccount.getText().toString().trim(),
                            etName.getText().toString().trim());
                } else {
                    Toasty.normal(activity, "请输入完整信息").show();
                }

            }
        });
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.showAtLocation(rlTitleBg, BOTTOM, 0, 0);
        popupView.startAnimation(animation);

    }


    @OnClick({R.id.ll_back, R.id.tv_balance_all, R.id.tv_add, R.id.btn_click})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_balance_all:
                etNum.setText(MainBActivity.User.getBalance());
                break;
            case R.id.tv_add:
                showPop();
                break;
            case R.id.btn_click:
                if (etNum.getText().toString().equals("")) {
                    Toasty.normal(activity, "请输入提现金额").show();
                    return;
                }
                if (alipayAccount == null || alipayAccount.equals("")) {
                    Toasty.normal(activity, "请添加支付宝账号").show();
                    showPop();
                    return;
                }
                Tools.showNormalDialogOne(activity, "提现", "确认提现", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mod5Dao.Presentation(MainBActivity.User.getUserId(), alipayAccount, alipayName, etNum.getText().toString().trim());
                    }
                }, null);
                break;
        }
    }


}
